package com.study.base.netty.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @description: MapperByteBufferTest 可以让文件直接在内存修改，操作系统不需要再拷贝一次
 * @date: 2021/1/21 16:57
 * @author: hcm
 * @version: 1.0
 */
public class MapperByteBufferTest {
    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt", "rw");

        //获取对应的通道
        FileChannel channel = randomAccessFile.getChannel();
        //FileChannel.MapMode.READ_WRITE 使用读写模式
        //0 可以直接修改1起始位置
        //5 隐射到内存的大小
        MappedByteBuffer mappedByteBuffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
        mappedByteBuffer.put(0, (byte) 'P');
        mappedByteBuffer.put(3, (byte) '9');
        randomAccessFile.close();
    }
}
